package com.valdemar.cookerybook.resources;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.valdemar.cookerybook.dao.CourseDao;
import com.valdemar.cookerybook.dao.IngredientDao;
import com.valdemar.cookerybook.model.Course;

@Path("/receipt/course")
public class CoursesResource
{

	@GET
	@Path("count")
	@Produces(MediaType.APPLICATION_JSON)
	public String getCount()
	{
		Integer count = CourseDao.instance.getCount();
		return Integer.toString(count);
	}

	// Return the list of all available courses
	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public List<Course> getCourses(@PathParam("languageId") int languageId)
	{
		List<Course> courses = new ArrayList<Course>();
		courses.addAll(CourseDao.instance.listAll(languageId));
		return courses;
	}

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public void addBulkCourses(
			@FormParam("courses") String courses, 
			@FormParam("languageId") int languageId, 
			@Context HttpServletResponse servletResponse) throws IOException
	{
		CourseDao.instance.addBulk(courses, languageId);
	}
	
}
